/**
 * @a https://leetcode.cn/problems/basic-calculator-ii/
 */

#include "common.h"

class Solution
{
public:
    int calculate(string s)
    {
        vector<int> nums;
        char op = '+';
        for (int i = 0; i < s.size();)
        {
            char ch = s[i];
            switch (ch)
            {
            case '+':
            case '-':
            case '*':
            case '/':
                op = ch;
                ++i;
                break;
            default: // 数字
            {
                int pos = s.find_first_of("+-*/", i);
                int cur_num = ::stoi(s.substr(i, pos - i));
                i = pos;
                if (op == '/')
                    nums.back() /= cur_num;
                else if (op == '*')
                    nums.back() *= cur_num;
                else
                    nums.push_back((op == '+' ? cur_num : -cur_num));
                break;
            }
            }
        }
        int ret = 0;
        for(auto num : nums)
            ret += num;

        return ret;
    }
};